var req;
var url;

function validate(action, target, type, redirect, parameters) {
	updatepage("processing","status");
	geturl(action, target, redirect, parameters);
	HttpProcess(target, type);
	updatepage("","status");
	if ((action == "validate_credentials" || action == "logout")&& target == "loginform"){
		updatepage("<div style='height:550px'>Please use the menu at the left side to navigate the site.</div>","main")
	}
		
}

function geturl(action, target, redirect, parameters){
	if (action == "validate_userid"){
	   var idField = document.getElementById("userid");
	   // call ValidateServlet
	   url = "/wedding/validate?action="+action+"&id=" + encodeURIComponent(idField.value);
	}
	if (action == "validate_password"){
	   var pwdField = document.getElementById("password");
	   // call ValidateServlet
	   url = "/wedding/validate?action="+action+"&password=" + encodeURIComponent(pwdField.value);
	}	
	if (action == "cleardiv"){
	   // call ValidateServlet
	   //url = "validate?action="+action+"&targetdiv="+target;
	   url = "/wedding/blank.html";
	}	
	if (action == "validate_credentials"){
	   var idField = document.getElementById("userid");
	   var pwdField = document.getElementById("password");
	   // call ValidateServlet
	   url = "/wedding/validate?action="+action+"&id=" + encodeURIComponent(idField.value) + "&password=" + encodeURIComponent(pwdField.value);
	   
	   //var url = "http://localhost:8080/iplecture03/initialize1";
	}
	if (action == "display_login"){
	   // call ValidateServlet
	   url = "/wedding/validate?action="+action;
	}	
	if (action == "filldiv"){
	   // call ValidateServlet
	   if (parameters == null){
	   		url = "/wedding/validate?action="+action+"&page="+redirect;
	   } else {
	   		url = "/wedding/validate?action="+action+"&page="+redirect+"&parameters="+parameters;
	   }
	}	
	if (action == "logout"){
	   // call ValidateServlet
	   url = "/wedding/validate?action="+action;
	}	
	if (action == "fillsearchdiv"){
	   	// call ValidateServlet
	   	url = "/wedding/validate?action="+action+"&page="+redirect+"&parameters="+parameters;
	}		
}

function HttpProcess(target, type){
	if (type == "html"){
		// this is used for filling content div with any html response
		httpPost(url, "val", target);
	} else {
		// this is used for special req/response using xml as content message
		if (typeof XMLHttpRequest != "undefined") {
		    req = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
		    req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		req.open("GET", url, true);		
		req.onreadystatechange = callbackxml;
		req.send(null);
	}
}

function httpPost(strURL, strvalue, nid) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('GET', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText,nid);
        }
    }
    self.xmlHttpReq.send(getquerystring(strvalue,nid));
}

function getquerystring(strvalue,nid) {
    qstr = 'w=' + escape(strvalue) + '&nid=' + escape(nid);  // NOTE: no '?' before querystring
	//qstr = 'w=' + escape(strvalue);  // NOTE: no '?' before querystring
    return qstr;
}

function updatepage(str,nid){
    document.getElementById(nid).innerHTML = str;
}

function callbackxml() {
    if (req.readyState == 4) {
        if (req.status == 200) {
            // update the HTML DOM based on whether or not message is valid
            parseXmlMessage();
            
        }
    }
}

function parseXmlMessage() {
 	// get message from xml
 	var status = req.responseXML.getElementsByTagName("status");
 	var targetdiv = req.responseXML.getElementsByTagName("targetdiv");
 	var message = req.responseXML.getElementsByTagName("message");
 	setMessage(status[0].childNodes[0].nodeValue, 
 		targetdiv[0].childNodes[0].nodeValue,
 		message[0].childNodes[0].nodeValue);
 	//setDivHtml("Content",this.xmlHttpReq.responseText);
}

function setMessage(status, targetdiv, message) {
    var mdiv = document.getElementById(targetdiv);
    if (message == null){
    	message = "";
    }
    if (status == "invalid") {
		mdiv.innerHTML = "<div class=\"error\">"+message+"</ div>";
    } else if (status == "valid") {
		mdiv.innerHTML = "<div class=\"acknowledgement\">"+message+"</ div>";
    } else if (status == "clear") {
    	mdiv.innerHTML = "<div> </ div>";
    } else if (status == "html") {
    	mdiv.innerHTML = message;
    }
}

function clear(){
	validate("cleardiv", "userIdMessage", "html");
	//setMessage("clear", "userIdMessage", "");
	//setMessage("clear", "passwordMessage", "");
}